<?php
//error_reporting(E_ALL | E_NOTICE);
define('ROOT_PATH', strtr(dirname( __FILE__ ) ."/",array('\\'=>'/')));

require_once ROOT_PATH."core/Init.php";


$dir	=	(dirname($_SERVER['SCRIPT_NAME'])?dirname($_SERVER['SCRIPT_NAME']):'');
$dir	=	str_replace('\\','/',$dir);
$uri	=	$_SERVER['REQUEST_URI'];

if($dir && $dir!='./'){
	if($dir[strlen($dir)-1]!='/'){
		$dir.='/';
	}
	
	$url=substr($uri,strlen($dir),strlen($uri)-strlen($dir));
}
unset($dir,$uri);

if(preg_match("/\.(jpg|png|gif|jpeg)$/is",$url)){//View thumb image with width & heigh
	if(preg_match("/^thumb\//is",$url)){
		$url_in = array(
			"'thumb/([0-9]+)_([0-9]+)/([a-zA-Z0-9-_./]+)'",	//list detail
		);
		
		$url_out = array(
			"\\1 \\2 \\3",
		);
			
		$url_filter = preg_replace($url_in, $url_out, $url);
		    
		$arr=explode(' ',$url_filter);
		
		if(count($arr)==3 && in_array($arr[0] . '_' . $arr[1] ,IMGlobal::$thumb_size)){
		//if(count($arr)==3){
			EnBacImage::thumbImageWithBackground(urldecode($arr[2]),(int)$arr[0],(int)$arr[1]);
			exit;
		}
	}
	elseif(preg_match("/^thumb_in\//is",$url)){
		$url_in = array(
		 	"'thumb_in/([0-9]+)_([0-9]+)/([a-zA-Z0-9-_./]+)'",	//list detail
		);
		
		$url_out = array(
			"\\1 \\2 \\3",
		);
			
		$url_filter = preg_replace($url_in, $url_out, $url);
		    
		$arr=explode(' ',$url_filter);
		if(count($arr)==3 && in_array($arr[0] . '_' . $arr[1] ,IMGlobal::$thumb_in_size)){
			EnBacImage::thumbImageNoBG(urldecode($arr[2]),(int)$arr[0],(int)$arr[1]);
			exit;
		}
	}
	elseif(preg_match("/^thumb_w\//is",$url)){//View thumb image with width & no limit size
		$url_in = array(
			"'thumb_w/([0-9]+)/([a-zA-Z0-9-_./]+)'",	//list detail
		);
		
		$url_out = array(
			"\\1 \\2",
		);
			
		$url_filter = preg_replace($url_in, $url_out, $url);
		    
		$arr=explode(' ',$url_filter);
		
		if(count($arr)==2 && in_array($arr[0],IMGlobal::$thumb_w_size)){
			EnBacImage::thumbImageWithBackground(urldecode($arr[1]),(int)$arr[0]);
			exit;
		}
	}
	elseif(preg_match("/^thumb_wl\//is",$url)){//View thumb image with width & limit size
		$url_in = array(
			"'thumb_wl/([0-9]+)/([a-zA-Z0-9-_./]+)'",	//list detail
		);
		
		$url_out = array(
			"\\1 \\2",
		);
			
		$url_filter = preg_replace($url_in, $url_out, $url);
		    
		$arr=explode(' ',$url_filter);
		
		if(count($arr)==2 && in_array($arr[0],IMGlobal::$thumb_wl_size)){
			EnBacImage::thumbImageWithBackground(urldecode($arr[1]),(int)$arr[0],0,1);
			exit;
		}
	}
	elseif(preg_match("/^thumb_max\//is",$url)){//View anh thumb max
		$url_in = array(
			 "'thumb_max/([a-zA-Z0-9-_./]+),(.*)\.(jpg|png|gif|jpeg)'",	//list detail
			);
			
		$url_out = array(
			"\\1.\\3 \\2",
		);
			
		$url_filter 	= preg_replace($url_in, $url_out, $url);
		    
		$arr		= explode(' ',$url_filter);
		
		if(count($arr)==2){//Kiem tra dieu kien view anh
			/*function base64_url_encode($input) {
				return strtr(base64_encode($input), '+/', '-_');
			}*/
			
			function base64_url_decode($input) {
				return base64_decode(strtr(str_replace('=','',$input), '-_,', '+/'));
			}
			
			$title	=	base64_url_decode($arr[1]);
			
			EnBacImage::thumbImageWithBackground(urldecode($arr[0]),0,0,0,1,$title,1);
			exit;
		}
		else{//Xem ảnh max ko phun text
			$url_in = array(
				"'thumb_max/([a-zA-Z0-9-_./]+)'",	//list detail
			);
			
			$url_out = array(
				"\\1",
			);
				
			$url_filter 	= preg_replace($url_in, $url_out, $url);
			    
			$arr		= explode(' ',$url_filter);
			
			if(count($arr)==1){//Kiem tra dieu kien view anh
				EnBacImage::thumbImageWithBackground(urldecode($arr[0]),0,0,0,1);
				exit;
			}
		}
	}
}
//header("Location: /enbac_logo.gif"); 
header("HTTP/1.0 404 Not Found"); 
echo "<h1>404 - Not Found!!!</h1>";

exit;

header("Location: /enbac_logo.gif"); 
//header("HTTP/1.0 404 Not Found"); 
//echo "<h1>404 - Not Found!<br />Return to <a href='http://enbac.com'>EnBac.com</a></h1>";
exit;
?>
